 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atm.controller;

/**
 *
 * @author Abderahman
 */
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.atm.domain.ATMTransaction;
import org.atm.service.ATMTransactionService;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import org.atm.domain.Account;
import org.atm.domain.Client;
import org.atm.service.AccountService;
import org.atm.service.ClientService;

@ManagedBean(name="transactionBean")
public class ATMTransactionBean  extends AbstractBean{

    private Account selectedAccount;

    @EJB
    private AccountService accountService;

    private ATMTransaction selectedTransaction= new ATMTransaction();
    @EJB
    private ClientService clientService;

    @EJB
    private ATMTransactionService transactionService;

    
    /** Creates a new instance of ClientBean */
    public ATMTransactionBean() {
    }
       public Account getSelectedAccount() {
        return selectedAccount;
    }

    public void setSelectedAccount(Account selectedAccount) {
        this.selectedAccount = selectedAccount;
    }
    public List<Account> getAccounts() {
        String cardNumber = getAuthenticatedCardNumber();
        Client client = clientService.getByATMCardNumber(cardNumber);

        return client.getAccounts();
    }
    public Account getAccount() {
        String cardNumber = getAuthenticatedCardNumber();
        Client client = clientService.getByATMCardNumber(cardNumber);

        return client.getAccounts().get(0);
    }
    public List<ATMTransaction> getATMTransactions() {
        return transactionService.getAllDepositeNew();
    }
    public List<ATMTransaction> getAllTransactions()
    {
        return transactionService.getAllTransactions(getAccount().getNumber());
    }
    public String listTransactions() {
        return "listTransactions";
    }
    public String approveTransaction() {
        transactionService.approveTransaction(selectedTransaction);
     //   addStatusMessage("Transaction " + selectedTransaction + " was approved");

       selectedAccount = accountService.getByAccountNumber(selectedTransaction.getAccountId());
       double amount = selectedAccount.getAmount();
       selectedAccount.setAmount(amount + selectedTransaction.getAmount());
       accountService.persist(selectedAccount);
      
        return "listTransactions";
    }

    public void rejectTransaction() {
        transactionService.rejectTransaction(selectedTransaction);
        addStatusMessage("Client " + selectedTransaction + " was rejected");
    }
   private void addStatusMessage(String message) {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
    }
 public ATMTransaction getSelectedTransaction() {
        return selectedTransaction;
    }

    public void setSelectedTransaction(ATMTransaction transaction) {
        this.selectedTransaction = transaction;
    }
    private String GetCurrentTime()
    {
         Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter=
        new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        return dateNow.toString();
    }
    public String saveDeposit() {
        if(selectedTransaction.getAmount()<1)
       {
           addStatusMessage("Deposit amount can't be -ve or zero");
           return null;
       }
        selectedTransaction.setAccountId(getAccount().getNumber());
        selectedTransaction.setTime(GetCurrentTime());
        selectedTransaction.setType("Deposit");
        transactionService.persist(selectedTransaction);
        return "DepositConfirmation?faces-redirect=true";
   
    }
    public String saveWithdraw() {
       
       if(selectedTransaction.getAmount()<1)
       {
           addStatusMessage("Withdrawal amount can't be -ve or zero");
           return null;
       }
       else if (selectedTransaction.getAmount()>getAccount().getAmount())
       {
           addStatusMessage("Withdrawal amount can't exceed your account amount");
           return null;
       }
       selectedAccount = getAccount();
       double amount = selectedAccount.getAmount();
       selectedAccount.setAmount(amount - selectedTransaction.getAmount());
       accountService.persist(selectedAccount);

       selectedTransaction.setAccountId(getAccount().getNumber());

        selectedTransaction.setTime(GetCurrentTime());
        selectedTransaction.setType("Withdraw");
        transactionService.persist(selectedTransaction);

        return "TransactionConfirmation?faces-redirect=true";
  
    }
    public String saveMoneyTransfer() {

        if(selectedTransaction.getAmount()<1)
       {
           addStatusMessage("Transfered money amount can't be -ve or zero");
           return null;
       }
       selectedAccount = getAccount();
       double amount = selectedAccount.getAmount();
       selectedAccount.setAmount(amount - selectedTransaction.getAmount());
       accountService.persist(selectedAccount);

        selectedTransaction.setAccountId(getAccount().getAccountId());
        selectedTransaction.setTime(GetCurrentTime());
        selectedTransaction.setType("MoneyTransfer");
        transactionService.persist(selectedTransaction);
        return "TransactionConfirmation?faces-redirect=true";
       
    }
    public String saveBillPayment() {

        if(selectedTransaction.getAmount()<1)
       {
           addStatusMessage("Transaction amount can't be -ve or zero");
           return null;
       }
       selectedAccount = getAccount();
       double amount = selectedAccount.getAmount();
       selectedAccount.setAmount(amount - selectedTransaction.getAmount());
       accountService.persist(selectedAccount);


        selectedTransaction.setAccountId(getAccount().getAccountId());
        selectedTransaction.setTime(GetCurrentTime());
        selectedTransaction.setType("BillPayment");
        transactionService.persist(selectedTransaction);
        return "TransactionConfirmation?faces-redirect=true";
       
    }
    public String cancel() {
        return "welcome";
    }


    public String Deposit() {
        return "DepositTransaction";
    }
    public String Withdraw() {
        return "WithdrawTransaction";
    }
    public String Transfer() {
        return "MoneyTranfer";
    }
    public String PayBills() {
        return "BillsPayment";
    }
    public String TransactionsHistory() {
        return "listTransactionsHistory";
    }

}
